using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace T.FAS.Runtime.ScheduleJob.SPI
{
    [Serializable]
    public class ScheduleJobDataMap: StringKeyDirtyFlagMap
    {
        /// <summary>
        /// Create an empty <see cref="ScheduleJobDataMap" />.
        /// </summary>
        public ScheduleJobDataMap()
        {
        }

        /// <summary> 
        /// Create a <see cref="ScheduleJobDataMap" /> with the given data.
        /// </summary>
        public ScheduleJobDataMap(IDictionary map)
            : this()
        {
            PutAll(map);
        }

        public Dictionary<string, object> GetDictionary()
        {
            var dic = new Dictionary<string, object>();
            if (this.Count > 0)
            {
                foreach (var key in Keys)
                {
                    dic[key.ToString()] = this[key];
                }
            }
            return dic;
        }


        /// <summary>
        /// Adds the given <see cref="bool" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, bool value)
        {
            string strValue = value.ToString();
            base.Put(key, strValue);
        }

        /// <summary>
        /// Adds the given <see cref="string" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void AddJsonData(string jsondata)
        {
            base.Put("TaskJsonData", jsondata);
        }

        /// <summary>
        /// Retrieve Json value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual string GetJsonData()
        {
            return base.GetString("TaskJsonData");
        }

        /// <summary>
        /// Adds the given <see cref="char" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, char value)
        {
            string strValue = value.ToString(CultureInfo.InvariantCulture);
            base.Put(key, strValue);
        }

        /// <summary>
        /// Adds the given <see cref="double" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, double value)
        {
            string strValue = value.ToString(CultureInfo.InvariantCulture);
            base.Put(key, strValue);
        }


        /// <summary>
        /// Adds the given <see cref="float" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, float value)
        {
            string strValue = value.ToString(CultureInfo.InvariantCulture);
            base.Put(key, strValue);
        }


        /// <summary>
        /// Adds the given <see cref="int" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, int value)
        {
            string strValue = value.ToString(CultureInfo.InvariantCulture);
            base.Put(key, strValue);
        }


        /// <summary>
        /// Adds the given <see cref="long" /> value as a string version to the
        /// <see cref="ITask" />'s data map.
        /// </summary>
        public virtual void PutAsString(string key, long value)
        {
            string strValue = value.ToString(CultureInfo.InvariantCulture);
            base.Put(key, strValue);
        }


        /// <summary>
        /// Retrieve the identified <see cref="int" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual int GetIntFromString(string key)
        {
            object obj = this[key];

            return Int32.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="int" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual int GetIntValue(string key)
        {
            object obj = this[key];

            if (obj is string)
            {
                return GetIntFromString(key);
            }
            else
            {
                return GetInt(key);
            }
        }

        /// <summary>
        /// Retrieve the identified <see cref="int" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual int GetIntegerFromString(string key)
        {
            object obj = this[key];

            return Int32.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="bool" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual bool GetBooleanValueFromString(string key)
        {
            object obj = this[key];

            return ((string)obj).ToUpper(CultureInfo.InvariantCulture).Equals("TRUE");
        }

        /// <summary>
        /// Retrieve the identified <see cref="bool" /> value from the 
        /// <see cref="TaskDataMap" />.
        /// </summary>
        public virtual bool GetBooleanValue(string key)
        {
            object obj = this[key];

            if (obj is string)
            {
                return GetBooleanValueFromString(key);
            }
            else
            {
                return GetBoolean(key);
            }
        }

        /// <summary>
        /// Retrieve the identified <see cref="Boolean" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual Boolean GetBooleanFromString(string key)
        {
            object obj = this[key];

            return ((string)obj).ToUpper(CultureInfo.InvariantCulture).Equals("TRUE");
        }

        /// <summary>
        /// Retrieve the identified <see cref="char" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual char GetCharFromString(string key)
        {
            object obj = this[key];

            return ((string)obj)[0];
        }

        /// <summary>
        /// Retrieve the identified <see cref="double" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual double GetDoubleValueFromString(string key)
        {
            object obj = this[key];
            return Double.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="double" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual double GetDoubleValue(string key)
        {
            object obj = this[key];

            if (obj is string)
            {
                return GetDoubleValueFromString(key);
            }
            else
            {
                return GetDouble(key);
            }
        }

        /// <summary>
        /// Retrieve the identified <see cref="Double" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual Double GetDoubleFromString(string key)
        {
            object obj = this[key];
            return Double.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="float" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual float GetFloatValueFromString(string key)
        {
            object obj = this[key];
            return Single.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="float" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual float GetFloatValue(string key)
        {
            object obj = this[key];

            if (obj is string)
            {
                return GetFloatValueFromString(key);
            }
            else
            {
                return GetFloat(key);
            }
        }

        /// <summary>
        /// Retrieve the identified <see cref="float" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual Single GetFloatFromString(string key)
        {
            object obj = this[key];
            return Single.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="long" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual long GetLongValueFromString(string key)
        {
            object obj = this[key];
            return Int64.Parse((string)obj, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Retrieve the identified <see cref="long" /> value from the <see cref="TaskDataMap" />.
        /// </summary>
        public virtual long GetLongValue(string key)
        {
            object obj = this[key];

            if (obj is string)
            {
                return GetLongValueFromString(key);
            }
            else
            {
                return GetLong(key);
            }
        }


        /// <summary>
        /// Gets the date time.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public DateTime GetDateTime(string key)
        {
            object obj = this[key];

            try
            {
                return (DateTime)obj;
            }
            catch (Exception)
            {

/* Unmerged change from project 'T.FAS.Runtime.ScheduleJob.SPI (net451)'
Before:
                throw new InvalidCastException(CommonStringClassSPI.ScheduleJobDataMap_GetDateTime);
After:
                throw new InvalidCastException(SPI.CommonStringClassDonotTranslated.ScheduleJobDataMap_GetDateTime);
*/
                throw new InvalidCastException(CommonStringClassDonotTranslated.ScheduleJobDataMap_GetDateTime);
            }
        }

        /// <summary>
        /// Gets the value behind the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public object Get(object key)
        {
            return this[key];
        }
    }
}
